//
// Created by francklinson on 2021/10/23 AT 10:38.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <functional>
#include <climits>
#include <string>
#include <chrono>

//using namespace std;
struct Timer {
    std::chrono::time_point<std::chrono::steady_clock> start, end;
    std::chrono::duration<float> duration;

    Timer() {
        start = std::chrono::high_resolution_clock::now();
    }

    ~Timer() {
        end = std::chrono::high_resolution_clock::now();
        duration = end - start;
        float ms = duration.count() * 1000.0f;
        std::cout << "Timer took " << ms << "ms" << std::endl;
    }
};

int main() {
    int *array = new int[25];
    int **a2d = new int *[5];
    for (int i = 0; i < 5; ++i) {
        a2d[i] = new int[5];
    }
    auto time1 = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            a2d[i][j] = 2;
        }
    }
    auto time2 = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            array[i * 5 + j] = 2;

        }
    }
    auto time3 = std::chrono::high_resolution_clock::now();
    std::chrono::duration<float> duration1 = time2 - time1;
    std::chrono::duration<float> duration2 = time3 - time2;
    std::cout << "time1: " << duration1.count() *1000000<< std::endl;
    std::cout << "time2: " << duration2.count()*1000000 << std::endl;
    // 在线性内存内读写 比 在非连续内存内读写 快得多

/*    int ***a3d = new int **[50];
    for (int i = 0; i < 50; ++i) {
        a3d[i] = new int*[50];
        for (int j = 0; j < 50; ++j) {
            a3d[i][j]=new int [50];
        }
    }*/
    // 删除
    delete[] array;

    for (int i = 0; i < 50; ++i) {
        delete[] a2d[i];
    }
    delete[] a2d;

    return 0;
}

